//
//  YFJourneyVC.m
//  project
//
//  Created by Winny Zhang on 2019/3/21.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFJourneyVC.h"
#import "HJTabBarViewController.h"
#import "YFJourneyModel.h"
#import "HJNotMatch.h"
#import "HJChatRoomVC.h"
#import "YFTeamedDetailVC.h"
#import "YFNotMatchDetailVC.h"
#import "YFMatchedDetailVC.h"

@interface YFJourneyVC ()

/**
 数据源数据
 */
@property (nonatomic, strong) NSArray <YFRecordModel *> *records;

/**
 导航右边按钮
 */
@property (nonatomic,strong) UIButton *rightNavBtn;

/**
 发布行程
 */
@property (nonatomic,strong) UIButton *journeyBtn;
@end

@implementation YFJourneyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navTitle = @"我的行程";
    self.records = [NSArray array];
    [self initRightBtn];
    [self loadNetworkData];
}

-(void)initRightBtn {
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    UIImage *image = setImage(@"lejian_journey");
    self.journeyBtn = btn;
    [btn setImage:image forState:UIControlStateNormal];
    btn.frame = CGRectMake(SCREEN_WIDTH - image.size.width - 25, SCREEN_HEIGTH-Height_NavBar - Height_TabBar - image.size.height - 30, image.size.width, image.size.height);
    @weakify(self)
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
//        [self publicVC];
    }];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
    
    UIButton * rightNavBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [rightNavBtn setTitleColor:[UIColor blackColor] forState:0];
    [rightNavBtn setTitle:@"修改" forState:0];
    rightNavBtn.hidden = YES;
    self.rightNavBtn = rightNavBtn;
    rightNavBtn.titleLabel.font = [UIFont systemFontOFSize15];
    rightNavBtn.frame = CGRectMake(0, 0, rightNavBtn.titleLabel.size.width, rightNavBtn.titleLabel.size.height);
    [[rightNavBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self didClickRightNavAction];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightNavBtn];
}

/**
 发布行程
 */
//-(void)publicVC {
//    [self.navigationController pushViewController:[YFPublishTripVC new] animated:YES];
//}
//
//-(void)didClickRightNavAction {
//    YFPublishTripVC * publishVC = [YFPublishTripVC new];
//    publishVC.recordModel = self.records[0];
//    publishVC.isEdite = YES;
//    [self.navigationController pushViewController:publishVC animated:YES];
//}

-(void)configTableView {
    self.needRefresh = YES;
    self.tableViewStyle = UITableViewStylePlain;
    [super configTableView];
    self.tableView.rowHeight = 180;
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGTH-Height_NavBar-Height_TabBar);
}


#pragma mark - 网络请求
-(void)loadNetworkData {
    NSString *postString = [Api stringByAppendingString:UserJourneyApi];
    NSDictionary *params = @{
                             @"userId": [HJUserManager getUserModel].userId,
                             @"pageCurrent":@(self.page),
                             @"tripStatus":@""
                             };
    @weakify(self)
    [HJHttpTool get:postString params:params success:^(HJResponeds *result) {
        @strongify(self)
        LKLog(@"%@", result);
        NSDictionary *dataDict = result.data;
        YFJourneyModel *model = [YFJourneyModel yy_modelWithDictionary:dataDict];
        
        self.totalPage = @(model.pages.intValue);
        self.records = self.records.addArray(model.records, self.tableView.mj_header.isRefreshing);
        dispatch_async(dispatch_get_main_queue(), ^{
            self.rightNavBtn.hidden = self.records.count == 0;
            self.journeyBtn.hidden = !self.rightNavBtn.hidden;
        });
        [self.tableView reloadData];
        [self.tableView endRefresh];
        // 到底了
        if (model.pages.integerValue != self.page) { [self.tableView.mj_footer endRefreshing]; }
        self.tableView.mj_footer.hidden = self.records.count == 0;
        
    } failure:^(id result) {
        @strongify(self)
        [self.tableView endRefresh];
        if (self.records.count == 0) {
            self.tipEmptyView.message = result;
            [self.tableView reloadData];
        } else {
            [YFHud showError:result];
        }
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJNotMatch * notMachCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!notMachCell) {
        notMachCell = [[NSBundle mainBundle]loadNibNamed:@"HJNotMatch" owner:nil options:nil].firstObject;
    }
    notMachCell.type = notMatch;
    if (_records && _records.count && indexPath.row < _records.count) {
        [notMachCell loadMatchCellDataSource:self.records[indexPath.row]];
    }
    @weakify(self)
    notMachCell.encounterBlock = ^(NSString *tripId) {
        @strongify(self)
        LKLog(@"未匹配行程id：%@", tripId);
        HJTabBarViewController * tabbar = (HJTabBarViewController *)self.tabBarController;
//        tabbar.playerVC.tripId = tripId;
//        [tabbar.playerVC loadNetworkData];
        [tabbar setSelectedIndex:0];
    };
   
    return notMachCell;
}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    YFRecordModel * recordModel = _records[indexPath.row];
//    switch (recordModel.tripModel.tripStatus.intValue) {
//        case 0:
//        {
//            YFNotMatchDetailVC *vc = [YFNotMatchDetailVC new];
//            vc.journeyModel = recordModel;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//            break;
//        case 1:
//        {
//            YFMatchedDetailVC *vc = [YFMatchedDetailVC new];
//            vc.journeyModel = recordModel;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//            break;
//        case 2:
//        {
//            YFTeamedDetailVC *vc = [YFTeamedDetailVC new];
//            vc.journeyModel = recordModel;
//            [self.navigationController pushViewController:vc animated:YES];
//        }
//            break;
//        default:
//            break;
//    }
//
//}

@end
